//*************************************************************************************************
// WebTestDependentFilter.cs
// Owner: Ed Glas
//
// This web test plugin filters dependents from a particular site.
// For example, if the site you are testing has ads served by another company
// you probably don't want to hit that site as part of a load test.
// This plugin enables you to filter all dependents from a particular site.
//
// Copyright(c) Microsoft Corporation, 2008
//*************************************************************************************************
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace SampleWebTestRules
{
    public class WebTestDependentFilter : WebTestPlugin
    {
        string m_startsWith;
        public string FilterDependentRequestsThatStartWith
        {
            get { return m_startsWith; }
            set { m_startsWith = value; }
        }

        public override void PostRequest(object sender, PostRequestEventArgs e)
        {
            WebTestRequestCollection depsToRemove = new WebTestRequestCollection();

            // Note, you can't modify the collection inside a foreach, hence the second collection
            // requests to remove.
            foreach (WebTestRequest r in e.Request.DependentRequests)
            {
                if (!string.IsNullOrEmpty(FilterDependentRequestsThatStartWith) && 
                    r.Url.StartsWith(FilterDependentRequestsThatStartWith))
                {
                    depsToRemove.Add(r);
                }
            }
            foreach (WebTestRequest r in depsToRemove)
            {
                e.Request.DependentRequests.Remove(r);
            }
        }
    }
}
